package com.dragon.concurrent.demo.demo;

public abstract class ObservableRunnable implements Runnable {

    final protected LifeCycleListener lifeCycleListener;

    public ObservableRunnable(final LifeCycleListener lifeCycleListener) {
        this.lifeCycleListener = lifeCycleListener;
    }

    protected void notifyChange(final RunnableEvent runnableEvent) {
        lifeCycleListener.onEvent(runnableEvent);
    }

    public enum RunnableState {
        RUNNING, DONE, ERROR;
    }

    public static class RunnableEvent {
        private final RunnableState state;

        private final Thread thread;

        private final Throwable cause;

        public RunnableEvent(RunnableState state, Thread thread, Throwable cause) {
            this.state = state;
            this.thread = thread;
            this.cause = cause;
        }

        public RunnableState getState() {
            return state;
        }

        public Thread getThread() {
            return thread;
        }

        public Throwable getCause() {
            return cause;
        }
    }
}
